package com.uchengwang.ui.cm_ui.cycle.ui;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;

import com.uchengwang.R;
import com.uchengwang.functions.http.config.BaseConfig;
import com.uchengwang.functions.http.listener.OnResultListener;
import com.uchengwang.ui.base_ui.ui.BaseWithTitleActivity;
import com.uchengwang.ui.cm_ui.cycle.http.CycleHttpUtils;
import com.uchengwang.ui.utils.UCommonUtil;

import de.greenrobot.event.EventBus;
import io.rong.imlib.model.Conversation;

/**
 * Created by U城网 on 2018/5/26.
 * 创建社圈时的邀请码
 */

public class InviteCodeCreateCycleActivity extends BaseWithTitleActivity implements View.OnClickListener{
    private EditText invite_edit;
    private Button invite_next;
    private LinearLayout invite_bm_linear;
    private String inviteCode;//邀请码

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        EventBus.getDefault().register(this);
        setContentView(R.layout.activity_invite_code_create_cycle);
        setTitle("邀请码");
        initView();
    }

    private void initView(){
        invite_edit = (EditText) findViewById(R.id.invite_edit);
        invite_next = (Button) findViewById(R.id.invite_next);
        invite_next.setOnClickListener(this);
        invite_bm_linear = (LinearLayout) findViewById(R.id.invite_bm_linear);
        invite_bm_linear.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.invite_next://下一步
                inviteCode = invite_edit.getText().toString();
                if (TextUtils.isEmpty(inviteCode)){
                    UCommonUtil.getUtil().showToast(this, "请输入邀请码");
                    return;
                }

                CycleHttpUtils.getUtils().checkInviteCode(this, inviteCode, new OnResultListener() {
                    @Override
                    public void onResult(int stutas, String msg, int total, String data) {
                        if (stutas ==0){
                            Intent intent = new Intent(InviteCodeCreateCycleActivity.this, EditInfoCreateCycleActivity.class);
                            intent.putExtra(BaseConfig.INVITE_CODE, inviteCode);
                            startActivity(intent);
                        }else {
                            UCommonUtil.getUtil().showToast(InviteCodeCreateCycleActivity.this, msg);
                        }
                    }
                });

                break;

            case R.id.invite_bm_linear:
                //联系客服号 跳转到李博的聊天界面
                UCommonUtil.getUtil().toChatActiviy(this, 43577, Conversation.ConversationType.PRIVATE, "客服");
                break;
        }
    }

    //发布社圈成功 finish
    public void onEvent(String con) {
        if (!TextUtils.isEmpty(con) && con.equals(BaseConfig.RELEASE_CYCLE_SUCCESS)){
            finish();
        }
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }
}
